﻿using Survey.DataAccessLayer;
using Survey.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebUI.Business
{
    public class QuestionFeedbackBusiness
    {
        #region 答卷页面
        QuestionFeedbackAccessor feedbackAccessor = new QuestionFeedbackAccessor();
        QuestionnaireAccessor questionnaireAccessor = new QuestionnaireAccessor();
        //api/PostQuestionFeedback
        public Object PostQuestionFeedback(List<int> IdCollection)
        {
            foreach (var item in IdCollection)
            {
                feedbackAccessor.Add(new QuestionFeedback
                {
                    QuestionOptionId = item
                });
            }
            bool isOk = feedbackAccessor.Save() > 0;
            return new { Result = isOk };
        }
        //api/GetQuestionnaireById
        public Object GetQuestionnaireById(int id)
        {
            try
            {
                Questionnaire questionnaire = questionnaireAccessor.GetQuestionnaire(id);
                //屏蔽用户信息
                questionnaire.RefUser = null;
                questionnaire.UserId = -1;
                return Units.DataProcess.Obj2Json(questionnaire);
            }
            catch (Exception)
            {

                return new Questionnaire
                {
                    Describe = "无此ID问卷，异常",
                    Id = id,
                    QuestionCollection = null,
                    Title = "无此ID问卷，异常",
                };
            }
        }
        //只有id=1的管理员才有权限获取所有问卷
        //否者只能查看自己的问卷
        // api/GetAllQuestionnaire
        public Object GetAllQuestionnaire(HttpSessionStateBase session)
        {
            var curentUserId = session["curentUserId"];
            if (curentUserId == null)
                return new { Title = "没有这个问卷", Id = -1 };
            //管理员
            if ((int)curentUserId == 1)
            {
                return questionnaireAccessor.Select(q => true).Select(qq => new { Title = qq.Title, Id = qq.Id });
            }
            return questionnaireAccessor.Select(q => q.UserId == (int)curentUserId).Select(qq => new { Title = qq.Title, Id = qq.Id });
        }

        #endregion
    }
}